import os
from moviepy import VideoFileClip
from transcriber import transcribe_audio
from video_editor import insert_materials
from material_scanner import select_and_build_material_db
from material_manager import  match_materials
DB_PATH = "material_db/material_db.sqlite"

def main():
    # 路径配置
    video_path = "video/map.mp4"
    audio_path = "temp_audio.wav"
    material_root = "materials"
    output_path = "output_video.mp4"

    # 1. 提取音频
    print("正在提取音频...")
    video = VideoFileClip(video_path)
    video_duration = video.duration  # 获取视频总时长
    print(f"视频总时长: {video_duration:.2f} 秒")
    video.audio.write_audiofile(audio_path, codec="pcm_s16le")

    # 2. 语音识别
    print("正在识别语音内容...")
    segments = transcribe_audio(audio_path)
    print("识别结果:", segments)

    #3. 提取关键词及时间点
    #keywords_set = {"科技", "风景", "动物"}  # 可根据需求扩展
    # keyword_times = extract_keywords_with_time(segments)
    # print("关键词时间点:", keyword_times)

    # 4. 加载素材库并匹配素材
    # 3. 构建素材库（首次运行）
    if not os.path.exists(DB_PATH):
        select_and_build_material_db()

    # 传递视频时长给match_materials函数，避免在视频开头和结尾5秒内匹配素材
    insert_list = match_materials(segments, 0.1,video_duration=video_duration)
    print("插入列表:", insert_list)

    # 5. 插入素材并输出
    insert_materials(video_path, insert_list, output_path)

if __name__ == "__main__":
    main()
